package com.zsr.chapter05.servlet;

import com.zsr.chapter05.bean.Book;
import com.zsr.chapter05.bean.BookDB;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

/**
 * @Author zsr
 * @Description 展示图书
 * @Date 2021/4/22 10:20
 */
public class ListBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        Collection<Book> books= BookDB.getAll();
        out.print("本站提供的图书有：<br/>");
        for (Book book : books) {
            String url ="/javaweb_task_demo/purchaseServlet?id="+book.getId();
            out.write(book.getName()+"<a href='"+url+"'>点击购买</a><br/>");
        }
    }
}
